package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.fs.api.FsApi;
import com.sdy.supermarket.biz.model.GsRes;
import com.sdy.supermarket.biz.service.GsHotResService;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.model.GsHotRes;
import com.sdy.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-08-16
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/gsHotRes")
public class GsHotResController extends BaseController {
    @Autowired
    private GsHotResService gsHotResService;
    @Autowired
    private FsApi fsApi;

    /**
     * 获取单条数据
     *
     * @param id
     * @return
     */
    @GetMapping("/getHotResById")
    public Response getHotResById(Integer id) {
        GsHotRes byId = gsHotResService.getById(id);
        if (StringUtil.isNotBlank(byId.getImgUrl()) && StringUtil.isNotEmpty(byId.getImgUrl())) {
            byId.setFileInfo(fsApi.getFileInfo(byId.getImgUrl()));
        } else {
            byId.setFileInfo(null);
        }
        return Response.success(byId);
    }

    /**
     * 新增&修改
     *
     * @param hotRes
     * @return
     */
    @PostMapping("/saveOrUpdateHotRes")
    public Response saveOrUpdateHotRes(@RequestBody GsHotRes hotRes) throws BizException {
        int saveOrUpdateHotRes = gsHotResService.saveOrUpdateHotRes(hotRes);
        return saveOrUpdateHotRes != 0 ? Response.success("操作成功") : Response.error("操作失败");
    }


    /**
     * 排序上升&下降
     *
     * @param map (id) (type 0 提升 1 下降)
     * @return
     */
    @PostMapping("/upOrDown")
    public Response sortUpOrDown(@RequestBody HashMap<String, Object> map) throws BizException {
        int i = gsHotResService.sortUpOrDown((Integer) map.get("id"), (Integer) map.get("type"));
        return i != 0 ? Response.success("修改成功") : Response.error("修改成功");
    }

    /**
     * 分页查询热门应用列表
     *
     * @return
     */
    @GetMapping("/pageList")
    public Response queryList(Page<GsHotRes> page) {
        return Response.success(gsHotResService.pageList(page));
    }

    /**
     * 根据ID删除数据，删除后需要批量修改受影响的所有数据排序
     *
     * @param map
     * @return
     */
    @PostMapping("/delById")
    public Response deleteById(@RequestBody HashMap<String, Object> map) throws BizException {
        int delete = gsHotResService.deleteById((Integer) map.get("id"));
        return delete != 0 ? Response.success("删除成功") : Response.error("删除失败");
    }
}
